﻿using System;
using System.Web;
using System.Web.UI;
using Ext.Net;


namespace KamLib
{
    public abstract class LogicPanel : UserControl
    {
        //public const string SameText = "&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;";
        public const string SameText = @"<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed metus nibh, sodales a, porta at, vulputate eget, dui. Pellentesque ut nisl. Maecenas tortor turpis, interdum non, sodales non, iaculis ac, lacus. Vestibulum auctor, tortor quis iaculis malesuada. In pellentesque nisl non sem. </p>";
        public const string SameText2 = SameText + SameText;
        public const string SameText3 = SameText + SameText + SameText;
        public const string SameText4 = SameText + SameText + SameText + SameText;
        
        public abstract UilController LogicNode { get; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            EnsureChildControls();
        }
    }

    public abstract class LogicPanel<TLogicNode> : LogicPanel
        where TLogicNode : UilController, new()
    {

        public readonly UilContext NodeContext = new UilContext();

        TLogicNode _logic;
        public TLogicNode Logic
        {
            get
            {
                if (_logic == null)
                {
                    _logic = NodeContext.GetNode<TLogicNode>();
                    _logic.VirtualPath = GetVirtualPath();
                    if (ClientID != _logic.LID)
                        throw new Exception("The LogicControl.ClientID '" + ClientID + "' must be equal LogicStore.ID '" + _logic.LID + "'.");
                }
                return _logic;
            }
        }
        public override UilController LogicNode { get { return Logic; } }

        public string GetVirtualPath()
        {
            return VirtualPathUtility.ToAbsolute(VirtualPathUtility.GetDirectory(AppRelativeVirtualPath));
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Logic.Register(Page);
        }

        //public Control RenderList(Control container)
        //{
        //    return Logic.GetList().Render("0", this,
        //        container ?? Page.Form, container != null ? RenderMode.AddTo : RenderMode.RenderTo
        //    );
        //}

    }

    public abstract class LogicControl : UserControl
    {
        public UilNode Logic { get; set; }

        public virtual void Initialize()
        {
            Logic.Details.Initialize(this);
        }

    }

}